//
// Created by ashCheng on 2023/2/2.
//

#ifndef SALES_DATA_H
#define SALES_DATA_H

#include <iostream>
#include <string>

struct Sales_data {
  std::string bookNo;
  unsigned units_sold = 0;
  double revenue = 0.0;

  void CalcRevenue(double price);
  void Print() const;
  double CalcAveragePrice() const;
  void SetData(const Sales_data& data);
  void AddData(const Sales_data& data);
};

void Sales_data::CalcRevenue(double price) {
  revenue = units_sold * price;
}

void Sales_data::Print() const {
  std::cout << bookNo << " " << units_sold << " "  << revenue << "";
  double average_price = CalcAveragePrice();
  if (average_price != 0.0) {
    std::cout << average_price << std::endl;
  } else {
    std::cout << "(no sales)" << std::endl;
  }
}

double Sales_data::CalcAveragePrice() const {
  if (units_sold != 0) {
    return revenue / units_sold;
  } else {
    return 0.0;
  }
}

void Sales_data::SetData(const Sales_data& data) {
  bookNo = data.bookNo;
  units_sold = data.units_sold;
  revenue = data.revenue;
}

void Sales_data::AddData(const Sales_data& data) {
  bookNo += data.bookNo;
  units_sold += data.units_sold;
  revenue += data.revenue;
}

#endif  // SALES_DATA_H
